import Vue from 'vue'
let AllLoading = ''
let moment = require("moment");
const _ = require('lodash');
//全局loading
function loadFn() {
	AllLoading = Vue.prototype.$loading({
		lock: true,
		text: 'Loading',
		spinner: 'el-icon-loading',
		background: 'rgba(0,0,0,.7)'
	});
}

//清除所有缓存
function removeAllStorage() {
	localStorage.removeItem('company_name')
	localStorage.removeItem('company_id')
	localStorage.removeItem('username')
	localStorage.removeItem('role')
	localStorage.removeItem('roleId')
	localStorage.removeItem('userInfo')
	localStorage.removeItem('liftDetailId')
	localStorage.removeItem('moreLiftIdList')
	localStorage.removeItem('menuActive')
	localStorage.removeItem('register_code')
	localStorage.removeItem('isUser')
	localStorage.removeItem('tabsLeft')
	localStorage.removeItem('roleOperation')
	localStorage.removeItem('roleCurrentId')
	localStorage.removeItem('frameworkId')
	localStorage.removeItem('choseLiftData')
	localStorage.removeItem('org_type')
	localStorage.removeItem('PeripheralsManId')
	localStorage.removeItem('eleLib')
	localStorage.removeItem('pt')
	localStorage.removeItem('jianKong')
	localStorage.removeItem('guZhang')
	localStorage.removeItem('tongJi')
	localStorage.removeItem('baoYang')
	localStorage.removeItem('weiXiu')
	localStorage.removeItem('yeWu')
	localStorage.removeItem('xiTong')
	localStorage.removeItem('web')
	localStorage.removeItem('ManufactureCompanyId')
	localStorage.removeItem('all_today_fault')
	localStorage.removeItem('all_access_count')
	localStorage.removeItem('all_elevator_count')
	localStorage.removeItem('currentInstallation_place')
	localStorage.removeItem('currentproperty_company_name')
	localStorage.removeItem('diagnoseId')
	localStorage.removeItem('diaRegister_code')
	localStorage.removeItem('diaInstallation_place')
	localStorage.removeItem('diaProperty_company_name')
	localStorage.removeItem('last_checkup_score')
	localStorage.removeItem('last_checkup_time')
	localStorage.removeItem('bendTime')
	localStorage.removeItem('runTimes')
	localStorage.removeItem('openTimes')
	localStorage.removeItem('upTime')
	localStorage.removeItem('newMess')
	localStorage.removeItem('BroadcastManageId')
	localStorage.removeItem('qtypeId')
	localStorage.removeItem('currentRowList')
	if (localStorage.surl) {
		localStorage.removeItem('surl')
	}
}

//get
function axiosGet(url, params = {}, success) {
	Vue.axios({
		url,
		method: 'get',
		params,
	}).then((res) => {
		success && success(res)
	}).catch((error) => {
		console.log(error)
	})
}

//post
function axiosPost(url, data = {}, success) {
	Vue.axios({
		url,
		method: 'post',
		data,
	}).then((res) => {
		success && success(res)
	}).catch((error) => {
		console.log(error)
	})
}

function noLoadingPost(url, data = {}, success) {
	Vue.axios({
		url,
		method: 'post',
		data,
	}).then((res) => {
		success && success(res)
	}).catch((error) => {
		console.log(error)
	})
}

function removeAlert(mess, alertFn) {
	this.$confirm(mess, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	}).then(() => {
		alertFn && alertFn()
	}).catch(() => {

	});
}

//delete
function axiosDelete(url, data = {}, success) {
	Vue.axios({
		url,
		method: 'delete',
		data,
	}).then((res) => {
		success && success(res)
	}).catch((error) => {
		console.log(error)
	})
}

//put
function axiosPut(url, data = {}, success) {
	Vue.axios({
		url,
		method: 'put',
		data,
	}).then((res) => {
		success && success(res)
	}).catch((error) => {
		console.log(error)
	})
}

//自配
function axiosChange(url, method, data = {}, success) {
	Vue.axios({
		url,
		method,
		data,
	}).then((res) => {
		success && success(res)
	}).catch((error) => {
		console.log(error)
	})
}


// function axiosGetFn(url, params = {}, success) {
// 	Vue.axios({
// 		url,
// 		method: 'get',
// 		params,
// 	}).then((res) => {
// 		console.log(res)
// 		if(res.code == 0){
// 			success && success(res.detail)
// 		}else{
// 			Vue.prototype.$message({
// 				type:'error',
// 				message:res.detail.result
// 			})
// 		}
// 	}).catch((error) => {
// 		console.log(error)
// 	})
// }


/**
 * 数据过滤
 * @param {array} data  数据
 * @param {String} type 数据粒度 day month quarter year
 */
function filter(data, type = 'day') {
	if (type !== 'day') {

		let _groupByMethod = null;
		if (type === 'month') {
			_groupByMethod = (item) => {
				return moment(item.date, 'YYYY-MM-DD').format('YYYY-MM')
			}
		} else if (type === 'quarter') {
			_groupByMethod = (item) => {
				const time = moment(item.date, 'YYYY-MM-DD')
				const quartarStr = `${time.format('YYYY')}-${time.quarter()}`
				// console.log(item.date, quartarStr)
				return quartarStr
			}
		} else { // year
			_groupByMethod = (item) => {
				return moment(item.date, 'YYYY-MM-DD').format('YYYY')
			}
		}

		const groupedData = _.groupBy(data, _groupByMethod)
		// console.log('groupedData', groupedData)
		const mappedData = _.map(groupedData, (items, mapKey) => {
			return _.reduce(items, (result, value, key) => {
				return {
					date: mapKey,
					value1: result.value1 + value.value1,
					value2: result.value2 + value.value2,
					value3: result.value3 + value.value3,
					value4: result.value4 + value.value4,
					value5: result.value5 + value.value5,
					value6: result.value6 + value.value6,
					value7: result.value7 + value.value7,
					value8: result.value8 + value.value8,
					value9: result.value9 + value.value9,
					value10: result.value10 + value.value10,
					value11: result.value11 + value.value11,
					value12: result.value12 + value.value12,
					value13: result.value13 + value.value13,
					value14: result.value14 + value.value14,
					value15: result.value15 + value.value15,
					value16: result.value16 + value.value16,
				}
			}, {
				value1: 0,
				value2: 0,
				value3: 0,
				value4: 0,
				value5: 0,
				value6: 0,
				value7: 0,
				value8: 0,
				value9: 0,
				value10: 0,
				value11: 0,
				value12: 0,
				value13: 0,
				value14: 0,
				value15: 0,
				value16: 0,
			})
		});
		return mappedData
	}
	return data
}


export {
	loadFn,
	axiosGet,
	axiosPost,
	axiosDelete,
	axiosPut,
	axiosChange,
	removeAllStorage,
	AllLoading,
	noLoadingPost,
	moment,
	filter,
	removeAlert
}
